﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xyz.BusinessObjects;
using Xyz.DataObjects.Interfaces;

namespace Xyz.DataObjects.Entity.Dao
{
    public class EntityFlightSeatDao : IFlightSeatDao
    {
         public Boolean  IsBookedSeat(int flightId, int aircraftSeatId)
        {
            using (var context = EntityObjectFactory.CreateContext())
            {
                var flightSeat = context.FlightSeatEntities.SingleOrDefault(x => x.FlightId == flightId && x.AircraftSeatId == aircraftSeatId);

                if (flightSeat == null)
                {
                    return false;
                }
                return true;
            }
        }

         public FlightSeat SaveBlockFlightSeat(FlightSeat flightSeat)
         {
             using (var context = EntityObjectFactory.CreateContext())
             {
                 FlightSeatEntity entity = (FlightSeatEntity)Mapper.Map(flightSeat);
                    context.FlightSeatEntities.AddObject(entity);
                    context.SaveChanges();
                    flightSeat.FlightSeatId = entity.FlightSeatId;
                    return flightSeat;
             }
         }

         public void CancelBlockFlightSeat(int flightSeatId)
         {
             using (var context = EntityObjectFactory.CreateContext())
             {
                 var flightSeat = context.FlightSeatEntities.Single(x => x.FlightSeatId == flightSeatId);
                 context.FlightSeatEntities.DeleteObject(flightSeat);

                 context.SaveChanges();
                
             }

         }
        

    
    }
}
